﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.Model;
using BF.IBLL.Dictionary;
using Base.Core.UnityFactory;
using WebDataControls;
using System.Configuration;

namespace BF.Web.pages.Dictionary.HospitalManagement
{
    public partial class HospitalEdit : BasePage
    {
        Idic_OrganizationBLL iBLL = IOCFactory.GetIOCResolve<Idic_OrganizationBLL>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //DivisionCode.RootAreaCode = "340100";
                BindDDL();//绑定下拉表列
                if (PKey != "")
                {
                    dic_Organization tempModel = iBLL.GetModel(new Guid(PKey));//获取实体
                    SetModel(tempModel);
                    dic_Organization tempModelHigh = iBLL.GetModel(tempModel.gHigherKey.Value);
                    if (tempModelHigh != null)
                    {
                        ddlfatherNodeName.Text = tempModelHigh.sOrganizationName;
                    }
                    else
                    {
                        ddlfatherNodeName.Text = "无上级机构";
                    }
                    ddlfatherNodeName.Attributes.Add("readonly", "readonly");
                    hidFatherName.Value = ddlfatherNodeName.Text;
                    hidFathergKey.Value = tempModel.gHigherKey.ToString();
                }
                else
                {
                    dic_Organization tempModelHigh = iBLL.GetModel(gfatherKey);
                    if (tempModelHigh != null)
                    {
                        ddlfatherNodeName.Text = tempModelHigh.sOrganizationName;
                        hidFathergKey.Value = tempModelHigh.gKey.ToString();
                    }
                    else
                    {
                        ddlfatherNodeName.Text = "无上级机构";
                    }
                    ddlfatherNodeName.Attributes.Add("readonly", "readonly");
                    hidFatherName.Value = ddlfatherNodeName.Text;
                }
                //查看页面
                if (!String.IsNullOrEmpty(Request.QueryString["State"]) && (Request.QueryString["State"] == "View"))
                {
                    lbtnAdd.Visible = false;
                    lbtnSave.Visible = false;
                    EnabledControls(this.Form);
                    //DivisionCode.Visible = false;
                    //txtDivisionCode.Text = DivisionCode.SelectedAreaName;// (DivisionCode.provinceName.Contains("请选择") ? "" : DivisionCode.provinceName) + (DivisionCode.cityName.Contains("请选择") ? "" : DivisionCode.cityName) + (DivisionCode.countryName.Contains("请选择") ? "" : DivisionCode.countryName) + (DivisionCode.townName.Contains("请选择") ? "" : DivisionCode.townName) + (DivisionCode.villageName.Contains("请选择") ? "" : DivisionCode.villageName);
                    //txtDivisionCode.ReadOnly = true;
                }
                else
                {
                    //txtDivisionCode.Visible = false;
                }
                //string fatherNodeName = Request.QueryString["sfatherNodeName"] ?? "";
                //ddlfatherNodeName.Text = fatherNodeName;
                //ddlfatherNodeName.Attributes.Add("readonly", "readonly");
                //hidFatherName.Value = fatherNodeName;
                //hidFathergKey.Value = gfatherKey.ToString();

            }
        }

        protected Guid gfatherKey
        {
            get
            {
                if (Request.QueryString["sfathergkey"] == null)
                {
                    return default(Guid);
                }
                else
                {
                    return new Guid(Request.QueryString["sfathergkey"].ToString());
                }
            }
        }
        /// <summary>
        /// 保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            dic_Organization model = new dic_Organization();

            if (PKey != "")//修改操作
            {
                dic_Organization tempModel = iBLL.GetModel(new Guid(PKey), this.txtOrganizationCode.Text);//获取验证实体
                if (tempModel != null)
                {
                    Messages("组织机构代码已存在，请重新填写！");
                    return;
                }
            }
            else//增加操作
            {
                dic_Organization tempModel = iBLL.GetModel(null, this.txtOrganizationCode.Text);//获取验证实体
                if (tempModel != null)
                {
                    Messages("组织机构代码已存在，请重新填写！");
                    return;
                }
            }

            if (PKey != "")//修改操作
            {
                dic_Organization tempModel = iBLL.GetModel(new Guid(PKey));//获取实体
                GetModel(tempModel);//从页面上获取输入的值
                tempModel.sAreaCode = "340100";
                //tempModel.sAreaCode = DivisionCode.SelectedAreaCode;
                //tempModel.gHigherKey = gfatherKey;
                if (iBLL.Update())//修改操作
                {
                    Messages("修改成功！", "function(){winer.parentDom.document.getElementById('ctl00_ContentPlaceHtml_hidFathergKey').value = '" + tempModel.gHigherKey + "';winer.parentDom.document.getElementById('ctl00_ContentPlaceHtml_hidSelectedKey').value = '" + tempModel.gKey + "';winer.RefurbishParent();}", this);
                }
                else
                {
                    Messages("修改失败！");
                }
            }
            else//增加操作
            {
                model.gKey = Guid.NewGuid();
                model = GetModel(model);
                model.sAreaCode = "340100";
                //model.sAreaCode = DivisionCode.SelectedAreaCode;
                model.gHigherKey = gfatherKey;
                model.iUse = 1;

                if (iBLL.Add(model))
                {
                    MessageBox("新增成功！", "function(e){winer.parentDom.document.getElementById('ctl00_ContentPlaceHtml_hidFathergKey').value = '" + model.gHigherKey + "';winer.parentDom.document.getElementById('ctl00_ContentPlaceHtml_hidSelectedKey').value = '" + model.gKey + "';winer.RefurbishParent();Update('" + model.gKey.ToString() + "');}");
                }
                else
                {
                    Messages("新增失败！");
                }
            }
        }

        /// <summary>
        /// 获取页面实体Model
        /// </summary>
        /// <param name="model"></param>
        protected dic_Organization GetModel(dic_Organization model)
        {
            model = (dic_Organization)WebUIHelper.GetControlsModel(model, "dic_Organization", this.Form);
            model.dOperator = DateTime.Now;
            model.sOperator = GetSessionCurrentUserInfo.sLoginName;

            return model;
        }

        /// <summary>
        /// 绑定实体Model到页面
        /// </summary>
        /// <param name="model"></param>
        protected void SetModel(dic_Organization model)
        {
            WebUIHelper.SetControlsValue(model, "dic_Organization", this.Form);

            if (!string.IsNullOrEmpty(model.sAreaCode))
            {
                //this.DivisionCode.SelectedAreaCode = model.sAreaCode;
            }
            else
            {
                dic_Organization tempModel = GetSessionCurrentUserInfo.dic_Organization;
                if (tempModel == null)
                {
                    //DivisionCode.SelectedAreaCode = "340100";
                }
                else
                {
                    //DivisionCode.SelectedAreaCode = tempModel.sAreaCode;
                }
            }

        }

        /// <summary>
        /// 从字典表加载相应的下拉列表
        /// </summary>
        protected void BindDDL()
        {
            //this.DivisionCode.SelectedAreaCode ="340100";
            // this.DivisionCode.SetHideControl(5);

            //绑定机构类型
            List<dic_Dict> dicList = GetDictList("sOrganizationType");
            this.ddlOrganizationType.DataSource = dicList;
            this.ddlOrganizationType.DataValueField = "sCode";
            this.ddlOrganizationType.DataTextField = "sName";
            this.ddlOrganizationType.DataBind();
            System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem
            {
                Value = "",
                Text = "--请选择--"
            };
            this.ddlOrganizationType.Items.Insert(0, li);
            //绑定机构级别
            List<dic_Dict> dicList2 = GetDictList("sOrganizationLevel");
            this.ddlOrganizationLevel.DataSource = dicList2.Where(P => P.sCode != "0").OrderBy(P => P.sCode).ToList();
            this.ddlOrganizationLevel.DataValueField = "sCode";
            this.ddlOrganizationLevel.DataTextField = "sName";
            this.ddlOrganizationLevel.DataBind();
            System.Web.UI.WebControls.ListItem li2 = new System.Web.UI.WebControls.ListItem
            {
                Value = "",
                Text = "--请选择--"
            };
            this.ddlOrganizationLevel.Items.Insert(0, li2);
            ////绑定直接上级机构编码
            //List<dic_Hospital> dicHospitalList = iBLL.GetHigherCode();
            //this.ddlHigherCode.DataSource = dicHospitalList;
            //this.ddlHigherCode.DataValueField = "gKey";
            //this.ddlHigherCode.DataTextField = "sOrganizationName";
            //this.ddlHigherCode.DataBind();
            //System.Web.UI.WebControls.ListItem li3 = new System.Web.UI.WebControls.ListItem
            //{
            //    Value = "",
            //    Text = "--请选择--"
            //};
            //this.ddlHigherCode.Items.Insert(0, li3);

        }

        /// <summary>
        /// 主键属性
        /// </summary>
        protected string PKey
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["gKey"]))
                {
                    return Request.QueryString["gKey"].ToString();
                }
                else
                {
                    return "";
                }
            }
        }



    }
}